home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / guile-ii.src / guile-ii / guile-src / libguile / strop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-13  |  7.3 KB  |  291 lines

  1. /* classes: src_files */
  2.  
  3. /*    Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this software; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "_scm.h"
  23.  
  24.  
  25.  
  26. #ifdef __STDC__
  27. int
  28. scm_i_index (SCM * str, SCM chr, int pos, int pos2, char * why)
  29. #else
  30. int
  31. scm_i_index (str, chr, pos, pos2, why)
  32.      SCM * str;
  33.      SCM chr;
  34.      int pos;
  35.      int pos2;
  36.      char * why;
  37. #endif
  38. {
  39.   char * p;
  40.   ASSERT (NIMP (*str) && ROSTRINGP (*str), *str, pos, why);
  41.   ASSERT (ICHRP (chr), chr, pos2, why);
  42.   p = index (CHARS (*str), ICHR (chr));
  43.   return (p
  44.       ? p - CHARS (*str)
  45.       : -1);
  46. }
  47.  
  48. #ifdef __STDC__
  49. int
  50. scm_i_rindex (SCM * str, SCM chr, int pos, int pos2, char * why)
  51. #else
  52. int
  53. scm_i_rindex (str, chr, pos, pos2, why)
  54.      SCM * str;
  55.      SCM chr;
  56.      int pos;
  57.      int pos2;
  58.      char * why;
  59. #endif
  60. {
  61.   char * p;
  62.   ASSERT (NIMP (*str) && ROSTRINGP (*str), *str, pos, why);
  63.   ASSERT (ICHRP (chr), chr, pos2, why);
  64.   p = rindex (CHARS (*str), ICHR (chr));
  65.   return (p
  66.       ? p - CHARS (*str)
  67.       : -1);
  68. }
  69.  
  70.  
  71. PROC (s_string_index, "string-index", 2, 0, 0, scm_string_index);
  72. #ifdef __STDC__
  73. SCM 
  74. scm_string_index (SCM str, SCM chr)
  75. #else
  76. SCM 
  77. scm_string_index (str, chr)
  78.      SCM str;
  79.      SCM chr;
  80. #endif
  81. {
  82.   int pos;
  83.   pos = scm_i_index (&str, chr, ARG1, ARG2, s_string_index);
  84.   return (pos < 0
  85.       ? BOOL_F
  86.       : MAKINUM (pos));
  87. }
  88.  
  89.  
  90. PROC (s_string_rindex, "string-rindex", 2, 0, 0, scm_string_rindex);
  91. #ifdef __STDC__
  92. SCM 
  93. scm_string_rindex (SCM str, SCM chr)
  94. #else
  95. SCM 
  96. scm_string_rindex (str, chr)
  97.      SCM str;
  98.      SCM chr;
  99. #endif
  100. {
  101.   int pos;
  102.   pos = scm_i_rindex (&str, chr, ARG1, ARG2, s_string_rindex);
  103.   return (pos < 0
  104.       ? BOOL_F
  105.       : MAKINUM (pos));
  106. }
  107.  
  108.  
  109. PROC (s_substring_move_left_x, "substring-move-left!", 2, 0, 1, scm_substring_move_left_x);
  110. #ifdef __STDC__
  111. SCM
  112. scm_substring_move_left_x (SCM str1, SCM start1, SCM args)
  113. #else
  114. SCM
  115. scm_substring_move_left_x (str1, start1, args)
  116.      SCM str1;
  117.      SCM start1;
  118.      SCM args;
  119. #endif
  120. {
  121.   SCM end1, str2, start2;
  122.   long i, j, e;
  123.   ASSERT (3==scm_ilength (args), args, WNA, s_substring_move_left_x);
  124.   end1 = CAR (args); args = CDR (args);
  125.   str2 = CAR (args); args = CDR (args);
  126.   start2 = CAR (args);
  127.   ASSERT (NIMP (str1) && STRINGP (str1), str1, ARG1, s_substring_move_left_x);
  128.   ASSERT (INUMP (start1), start1, ARG2, s_substring_move_left_x);
  129.   ASSERT (INUMP (end1), end1, ARG3, s_substring_move_left_x);
  130.   ASSERT (NIMP (str2) && STRINGP (str2), str2, ARG4, s_substring_move_left_x);
  131.   ASSERT (INUMP (start2), start2, ARG5, s_substring_move_left_x);
  132.   i = INUM (start1), j = INUM (start2), e = INUM (end1);
  133.   ASSERT (i <= LENGTH (str1) && i >= 0, start1, OUTOFRANGE, s_substring_move_left_x);
  134.   ASSERT (j <= LENGTH (str2) && j >= 0, start2, OUTOFRANGE, s_substring_move_left_x);
  135.   ASSERT (e <= LENGTH (str1) && e >= 0, end1, OUTOFRANGE, s_substring_move_left_x);
  136.   ASSERT (e-i+j <= LENGTH (str2), start2, OUTOFRANGE, s_substring_move_left_x);
  137.   while (i<e) CHARS (str2)[j++] = CHARS (str1)[i++];
  138.   return UNSPECIFIED;
  139. }
  140.  
  141.  
  142. PROC (s_substring_move_right_x, "substring-move-right!", 2, 0, 1, scm_substring_move_right_x);
  143. #ifdef __STDC__
  144. SCM
  145. scm_substring_move_right_x (SCM str1, SCM start1, SCM args)
  146. #else
  147. SCM
  148. scm_substring_move_right_x (str1, start1, args)
  149.      SCM str1;
  150.      SCM start1;
  151.      SCM args;
  152. #endif
  153. {
  154.   SCM end1, str2, start2;
  155.   long i, j, e;
  156.   ASSERT (3==scm_ilength (args), args, WNA, s_substring_move_right_x);
  157.   end1 = CAR (args); args = CDR (args);
  158.   str2 = CAR (args); args = CDR (args);
  159.   start2 = CAR (args);
  160.   ASSERT (NIMP (str1) && STRINGP (str1), str1, ARG1, s_substring_move_right_x);
  161.   ASSERT (INUMP (start1), start1, ARG2, s_substring_move_right_x);
  162.   ASSERT (INUMP (end1), end1, ARG3, s_substring_move_right_x);
  163.   ASSERT (NIMP (str2) && STRINGP (str2), str2, ARG4, s_substring_move_right_x);
  164.   ASSERT (INUMP (start2), start2, ARG5, s_substring_move_right_x);
  165.   i = INUM (start1), j = INUM (start2), e = INUM (end1);
  166.   ASSERT (i <= LENGTH (str1) && i >= 0, start1, OUTOFRANGE, s_substring_move_right_x);
  167.   ASSERT (j <= LENGTH (str2) && j >= 0, start2, OUTOFRANGE, s_substring_move_right_x);
  168.   ASSERT (e <= LENGTH (str1) && e >= 0, end1, OUTOFRANGE, s_substring_move_right_x);
  169.   ASSERT ((j = e-i+j) <= LENGTH (str2), start2, OUTOFRANGE, s_substring_move_right_x);
  170.   while (i<e) CHARS (str2)[--j] = CHARS (str1)[--e];
  171.   return UNSPECIFIED;
  172. }
  173.  
  174.  
  175. PROC (s_substring_fill_x, "substring-fill!", 2, 0, 1, scm_substring_fill_x);
  176. #ifdef __STDC__
  177. SCM
  178. scm_substring_fill_x (SCM str, SCM start, SCM args)
  179. #else
  180. SCM
  181. scm_substring_fill_x (str, start, args)
  182.      SCM str;
  183.      SCM start
  184.      SCM args;
  185. #endif
  186. {
  187.   SCM end, fill;
  188.   long i, e;
  189.   char c;
  190.   ASSERT (2==scm_ilength (args), args, WNA, s_substring_fill_x);
  191.   end = CAR (args); args = CDR (args);
  192.   fill = CAR (args);
  193.   ASSERT (NIMP (str) && STRINGP (str), str, ARG1, s_substring_fill_x);
  194.   ASSERT (INUMP (start), start, ARG2, s_substring_fill_x);
  195.   ASSERT (INUMP (end), end, ARG3, s_substring_fill_x);
  196.   ASSERT (ICHRP (fill), fill, ARG4, s_substring_fill_x);
  197.   i = INUM (start), e = INUM (end);c = ICHR (fill);
  198.   ASSERT (i <= LENGTH (str) && i >= 0, start, OUTOFRANGE, s_substring_fill_x);
  199.   ASSERT (e <= LENGTH (str) && e >= 0, end, OUTOFRANGE, s_substring_fill_x);
  200.   while (i<e) CHARS (str)[i++] = c;
  201.   return UNSPECIFIED;
  202. }
  203.  
  204.  
  205. PROC (s_string_null_p, "string-null?", 1, 0, 0, scm_string_null_p);
  206. #ifdef __STDC__
  207. SCM
  208. scm_string_null_p (SCM str)
  209. #else
  210. SCM
  211. scm_string_null_p (str)
  212.      SCM str;
  213. #endif
  214. {
  215.   ASSERT (NIMP (str) && STRINGP (str), str, ARG1, s_string_null_p);
  216.   return (LENGTH (str)
  217.       ? BOOL_F
  218.       : BOOL_T);
  219. }
  220.  
  221.  
  222. PROC (s_string_to_list, "string->list", 1, 0, 0, scm_string_to_list);
  223. #ifdef __STDC__
  224. SCM
  225. scm_string_to_list (SCM str)
  226. #else
  227. SCM
  228. scm_string_to_list (str)
  229.      SCM str;
  230. #endif
  231. {
  232.   long i;
  233.   SCM res = EOL;
  234.   unsigned char *src;
  235.   ASSERT (NIMP (str) && STRINGP (str), str, ARG1, s_string_to_list);
  236.   src = UCHARS (str);
  237.   for (i = LENGTH (str)-1;i >= 0;i--) res = scm_cons ((SCM)MAKICHR (src[i]), res);
  238.   return res;
  239. }
  240.  
  241.  
  242.  
  243. PROC (s_string_copy, "string-copy", 1, 0, 0, scm_string_copy);
  244. #ifdef __STDC__
  245. SCM
  246. scm_string_copy (SCM str)
  247. #else
  248. SCM
  249. scm_string_copy (str)
  250.      SCM str;
  251. #endif
  252. {
  253.   ASSERT (NIMP (str) && STRINGP (str), str, ARG1, s_string_copy);
  254.   return scm_makfromstr (CHARS (str), (sizet)LENGTH (str), 0);
  255. }
  256.  
  257.  
  258. PROC (s_string_fill_x, "string-fill!", 2, 0, 0, scm_string_fill_x);
  259. #ifdef __STDC__
  260. SCM
  261. scm_string_fill_x (SCM str, SCM chr)
  262. #else
  263. SCM
  264. scm_string_fill_x (str, chr)
  265.      SCM str;
  266.      SCM chr;
  267. #endif
  268. {
  269.   register char *dst, c;
  270.   register long k;
  271.   ASSERT (NIMP (str) && STRINGP (str), str, ARG1, s_string_fill_x);
  272.   ASSERT (ICHRP (chr), chr, ARG2, s_string_fill_x);
  273.   c = ICHR (chr);
  274.   dst = CHARS (str);
  275.   for (k = LENGTH (str)-1;k >= 0;k--) dst[k] = c;
  276.   return UNSPECIFIED;
  277. }
  278.  
  279.  
  280. #ifdef __STDC__
  281. void
  282. scm_init_strop (void)
  283. #else
  284. void
  285. scm_init_strop ()
  286. #endif
  287. {
  288. #include "strop.x"
  289. }
  290.  
  291.